# coding=utf-8
##
## Author: jmdvirus@aliyun.com
##
## Create: 2019年02月18日 星期一 12时59分28秒
##

import cv2
import matplotlib.pyplot as plt
import numpy as np

basefile = '/opt/data/data/av.testav/1.jpg'

def baseop(file):
    img_bgr = cv2.imread(file)

    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

    plt.figure(figsize=(12, 6))
    plt.subplot(121)
    plt.imshow(img_bgr)
    plt.subplot(122)
    plt.imshow(img_rgb)
    plt.imshow(img_rgb)

def harris(file):
    img_bgr = cv2.imread(file)
    img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
    corners = cv2.cornerHarris(img_gray, 2, 3, 0.04)
    plt.imshow(corners, cmap='gray')

def sift(file):
    img_bgr = cv2.imread(file)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    sift = cv2.xfeatures2d.SIFT_create()
    kp = sift.detect(img_bgr)
    img_kp = np.zeros_like(img_bgr)
    img_kp = cv2.drawKeypoints(img_rgb, kp, img_kp, 
            flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    kp, des = sift.compute(img_bgr, kp)
    print("des: ", des.shape)
    plt.imshow(img_kp)

def surf(file):
    img_bgr = cv2.imread(file)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    surf = cv2.xfeatures2d.SURF_create()
    kp = surf.detect(img_bgr)
    img_kp = np.zeros_like(img_bgr)
    img_kp = cv2.drawKeypoints(img_rgb, kp, img_kp,
            flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    plt.imshow(img_kp)

if __name__ == "__main__":
    #baseop(basefile)
    #harris(basefile)
    #sift(basefile)
    surf(basefile)

    plt.show()

